package com.gaojinqi.sys.core.auth.aop;

import com.gaojinqi.sys.core.auth.annotation.Permission;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import java.lang.reflect.Method;

/**
 * 权限检查的aop
 *
 * @author fengshuonan
 * @date 2017-07-13 21:05
 */
@Aspect
@Order(200)
public class PermissionAop {

    @Pointcut(value = "@annotation(com.gaojinqi.sys.core.auth.annotation.Permission)")
    private void pointcutOfPermission() {

    }

    @Around("pointcutOfPermission()")
    public Object doPermission(ProceedingJoinPoint point) throws Throwable {
        Permission permission;
        // 切方法，优先于切类
        MethodSignature ms = (MethodSignature) point.getSignature();
        Method method = ms.getMethod();
        permission = method.getAnnotation(Permission.class);
        if (permission == null) {
            // 切类
            Class<?> invokeClass = point.getTarget().getClass();
            permission = invokeClass.getAnnotation(Permission.class);
        }
        String[] roleName = permission.roleName();
        if (roleName.length == 0) {

            //检查请求路径是否有访问权限 todo
            /*boolean result = authService.checkAll();
            if (result) {
                return point.proceed();
            } else {
                throw new PermissionException();
            }*/

        } else {

            //检查是否指定角色
            /*boolean result = authService.check(permissions);
            if (result) {
                return point.proceed();
            } else {
                throw new PermissionException();
            }*/
        }
        // todo 待实现
        return point.proceed();
    }

}
